import { promisify } from '../../utils/promise.util'
import { $init, $digest } from '../../utils/common.util'

const app = getApp()
Page({
  data: {
    id:"",
    name: "",
    phone: "",
    conpanyName: "",
    hangyeindex: 0,
    hangye: [],
    project: "",
    xuqiuindex: 0,
    xuqiu: [],
    xuqiu2index: 0,
    xuqiu2: [],
    mianjiindex: 0,
    mianji: [],
    mianjiAll: [],
    region: ["", "", ""],
    qudao: "",
    content: "",
    images: [],
    upload: [],
    pingfangmi: "m²",
    companyShow: true,
  },
  onLoad: function (option) {
    $init(this)

    this.setData({
      id:option.id
    })

    //获取行业数据
    var that = this;
    wx.request({
      url: app.globalData.httpurl + 'industry',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        that.setData({
          hangye: res.data.data
        })
      }
    })

    //获取面积数据
    wx.request({
      url: app.globalData.httpurl + 'area',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        var datas = res.data.data;
        for (var i = 0; i < datas.length; i++) {
          datas[i]["name"] = datas[i]["name"] + that.data.pingfangmi
        }
        that.setData({
          mianjiAll: datas,
        })

        //获取个人信息
        wx.request({
          url: app.globalData.httpurl + 'coustomer/detail',
          method: "GET",
          header: {
            "Authorization": "bearer " + app.globalData.access_token
          },
          data: {
            "customer_id": option.id
          },
          success: function (res) {
            var datas = res.data.data; //res.data就是从后台接收到的值

            // var hangyeS = datas.industry.name == null ? '' : datas.industry.name
            // var mianjiS = datas.area.name == null ? '' : datas.area.name
            // var xuqiuS = datas.demand.name == null ? '' : datas.demand.name

            that.setData({
              name: datas.name,
              phone: datas.mobile,//不可修改
              conpanyName: datas.company_name,
              companyShow: datas.project.name == '招商采盘' ? false : true,
              region: [datas.city1, datas.city2, ""],
              project: datas.project == null ? "" : datas.project.name,//不可修改
              qudao: datas.source == null ? "" : datas.source.name,//不可修改
              content: datas.beizhu == null ? "" : datas.beizhu.desc,
            })

            if (!that.data.companyShow) {
              that.setData({
                mianji: []
              })
              for (var i = 0; i < that.data.mianjiAll.length; i++) {

                var item = "mianjiAll[" + i + "].name";
                that.setData({
                  [item]: that.data.mianjiAll[i].name.replace("m²", "亩")
                })
                if (that.data.mianjiAll[i].is_display == 1) {
                  var length = that.data.mianji.length
                  that.data.mianji[length] = that.data.mianjiAll[i]
                  that.setData({
                    mianji: that.data.mianji
                  })
                }
              }
            } else {
              that.setData({
                mianji: []
              })
              for (var i = 0; i < that.data.mianjiAll.length; i++) {
                var item = "mianjiAll[" + i + "].name";
                that.setData({
                  [item]: that.data.mianjiAll[i].name.replace("亩", "m²")
                })

                var length = that.data.mianji.length
                that.data.mianji[length] = that.data.mianjiAll[i]
                that.setData({
                  mianji: that.data.mianji
                })
              }
            }

          }
        })
      }
    })

    //获取需求数据
    wx.request({
      url: app.globalData.httpurl + 'demand',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        var datas = res.data.data;
        that.setData({
          xuqiu: datas
        })
      }
    })

    //获取需求数据2
    wx.request({
      url: app.globalData.httpurl + 'demand',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        type: 1
      },
      success: function (res) {
        that.setData({
          xuqiu2: res.data
        })
      }
    })

   
  },
  chooseImage(e) {
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        const images = this.data.images.concat(res.tempFilePaths)
        // 限制最多只能留下3张照片
        this.data.images = images.length <= 1 ? images : images.slice(0, 3)
        $digest(this)

        this.uploadImg();
      }
    })
  },
  removeImage(e) {
    const idx = e.target.dataset.idx
    this.data.images.splice(idx, 1)
    $digest(this)
  },
  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.images
    wx.previewImage({
      current: images[idx],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },
  uploadImg: function () {
    var that = this;
    wx.uploadFile({
      url: app.globalData.httpurl + 'upload',
      filePath: this.data.images[0],
      name: 'images',
      success: function (res) {
        var result = JSON.parse(res.data);
        if (result.status == 1) {
          that.data.upload.push(result.data);
        }
      }
    })
  },
  bindname: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindphone: function (e) {
    var inputPhone = e.detail.value
    this.setData({
      tishi: false
    })
    if (inputPhone.length == 11){
      //检查号码可用性

      //如果号码不可用进行提示
      // this.setData({
      //   tishi:true
      // })
      this.setData({
        phone: inputPhone
      })
    }
  },
  bindconpanyName: function (e) {
    this.setData({
      conpanyName: e.detail.value
    })
  },
  bindhangye: function (e) {
    this.setData({
      hangyeindex: e.detail.value
    })
  },
  bindxuqiu: function (e) {
    this.setData({
      xuqiuindex: e.detail.value
    })
  },
  bindxuqiu2: function (e) {
    this.setData({
      xuqiu2index: e.detail.value
    })
  },
  bindmianji: function (e) {
    this.setData({
      mianjiindex: e.detail.value
    })
  },
  bindregion: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindcontent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  subInfo: function (e) {
    if (this.data.name == '') {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // if (this.data.conpanyName == '') {
    //   wx.showToast({
    //     title: '公司名称不能为空',
    //     icon: 'none',
    //     duration: 1500
    //   })
    //   return;
    // }
    if (this.data.content == '') {
      wx.showToast({
        title: '跟进情况不能为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    //提交数据
    wx.request({
      url: app.globalData.httpurl + 'customer/update',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      method: "POST",
      data: {
        "customer_id": this.data.id,
        "name": this.data.name,
        "company_name": this.data.conpanyName,
        "industry_id": this.data.companyShow ? this.data.hangye[this.data.hangyeindex].industry_id: "",//行业
        "demand_id": this.data.companyShow ? this.data.xuqiu[this.data.xuqiuindex].id:this.data.xuqiu2[this.data.xuqiu2index].id,//需求类型
        "area_id": this.data.mianji[this.data.mianjiindex].id,//需求面积
        "city1": this.data.region[0],
        "city2": this.data.region[1],
        // address: this.data.address,//地址
        "desc": this.data.content,//备注
        "image": this.data.upload.length == 0 ? "" : this.data.upload[0],//图片
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.navigateBack({
            delta: 1
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  }
})